<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="android,code," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.2" />






<meta name="description" content="1. 12345// Create a RealmConfiguration that saves the Realm file in the app&amp;apos;s &amp;quot;files&amp;quot; directory.RealmConfiguration realmConfig = new RealmConfiguration.Builder(context).build();Realm.se">
<meta name="keywords" content="android,code">
<meta property="og:type" content="article">
<meta property="og:title" content="Realm使用笔记">
<meta property="og:url" content="http://yoursite.com/2017/04/13/android/realm使用/index.html">
<meta property="og:site_name" content="The Old Man and the Sea">
<meta property="og:description" content="1. 12345// Create a RealmConfiguration that saves the Realm file in the app&amp;apos;s &amp;quot;files&amp;quot; directory.RealmConfiguration realmConfig = new RealmConfiguration.Builder(context).build();Realm.se">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2017-08-09T10:12:44.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Realm使用笔记">
<meta name="twitter:description" content="1. 12345// Create a RealmConfiguration that saves the Realm file in the app&amp;apos;s &amp;quot;files&amp;quot; directory.RealmConfiguration realmConfig = new RealmConfiguration.Builder(context).build();Realm.se">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2017/04/13/android/realm使用/"/>





  <title>Realm使用笔记 | The Old Man and the Sea</title>
  














</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">The Old Man and the Sea</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">But man is not made for defeat. A man can be destroyed but not defeated.</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/04/13/android/realm使用/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Ernest Chang">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/uploads/images/avert.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="The Old Man and the Sea">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Realm使用笔记</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-04-13T14:22:45+08:00">
                2017-04-13
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/android/" itemprop="url" rel="index">
                    <span itemprop="name">android</span>
                  </a>
                </span>

                
                
                  ， 
                
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/android/code/" itemprop="url" rel="index">
                    <span itemprop="name">code</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>1.</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">// Create a RealmConfiguration that saves the Realm file in the app&apos;s &quot;files&quot; directory.</div><div class="line">RealmConfiguration realmConfig = new RealmConfiguration.Builder(context).build();</div><div class="line">Realm.setDefaultConfiguration(realmConfig);</div><div class="line">// Get a Realm instance for this thread</div><div class="line">Realm realm = Realm.getDefaultInstance();</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">// The RealmConfiguration is created using the builder pattern.</div><div class="line">// The Realm file will be located in Context.getFilesDir() with name &quot;myrealm.realm&quot;</div><div class="line">RealmConfiguration config = new RealmConfiguration.Builder(context)</div><div class="line">  .name(&quot;myrealm.realm&quot;)</div><div class="line">  .encryptionKey(getKey())</div><div class="line">  .schemaVersion(42)</div><div class="line">  .modules(new MySchemaModule())</div><div class="line">  .migration(new MyMigration())</div><div class="line">  .build();</div><div class="line">// Use the config</div><div class="line">Realm realm = Realm.getInstance(config);</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line">RealmConfiguration myConfig = new RealmConfiguration.Builder(context)</div><div class="line">  .name(&quot;myrealm.realm&quot;)</div><div class="line">  .schemaVersion(2)</div><div class="line">  .modules(new MyCustomSchema())</div><div class="line">  .build();</div><div class="line">RealmConfiguration otherConfig = new RealmConfiguration.Builder(context)</div><div class="line">  .name(&quot;otherrealm.realm&quot;)</div><div class="line">  .schemaVersion(5)</div><div class="line">  .modules(new MyOtherSchema())</div><div class="line">  .build();</div><div class="line">Realm myRealm = Realm.getInstance(myConfig);</div><div class="line">Realm otherRealm = Realm.getInstance(otherConfig);</div><div class="line">public class MyApplication extends Application &#123;</div><div class="line">  @Override</div><div class="line">  public void onCreate() &#123;</div><div class="line">    super.onCreate();</div><div class="line">    // The Realm file will be located in Context.getFilesDir() with name &quot;default.realm&quot;</div><div class="line">    RealmConfiguration config = new RealmConfiguration.Builder(this).build();</div><div class="line">    Realm.setDefaultConfiguration(config);</div><div class="line">  &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">public class MyActivity extends Activity &#123;</div><div class="line">  @Override</div><div class="line">  protected void onCreate(Bundle savedInstanceState) &#123;</div><div class="line">    super.onCreate(savedInstanceState);</div><div class="line">    Realm realm = Realm.getDefaultInstance();</div><div class="line">    // ... Do something ...</div><div class="line">    realm.close();</div><div class="line">  &#125;</div><div class="line">&#125;</div><div class="line">​`</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">@RealmClass</div><div class="line">public class User implements RealmModel &#123;</div><div class="line">&#125;</div><div class="line">// With RealmObject</div><div class="line">user.isValid();</div><div class="line">user.addChangeListener(listener);</div><div class="line">// With RealmModel</div><div class="line">RealmObject.isValid(user);</div><div class="line">RealmObject.addChangeListener(user, listener);</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">final MyObject obj = new MyObject();</div><div class="line">obj.setId(42);</div><div class="line">obj.setName(&quot;Fish&quot;);</div><div class="line">realm.executeTransaction(new Realm.Transaction() &#123;</div><div class="line">    @Override</div><div class="line">    public void execute(Realm realm) &#123;</div><div class="line">        // This will create a new object in Realm or throw an exception if the</div><div class="line">        // object already exists (same primary key)</div><div class="line">        // realm.copyToRealm(obj);</div><div class="line">        // This will update an existing object with the same primary key</div><div class="line">        // or create a new object if an object with no primary key = 42</div><div class="line">        realm.copyToRealmOrUpdate(obj);</div><div class="line">    &#125;</div><div class="line">&#125;);</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">realm.beginTransaction();</div><div class="line">User user = realm.createObject(User.class); // Create a new object</div><div class="line">user.setName(&quot;John&quot;);</div><div class="line">user.setEmail(&quot;john@corporation.com&quot;);</div><div class="line">realm.commitTransaction();</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">User user = new User(&quot;John&quot;);</div><div class="line">user.setEmail(&quot;john@corporation.com&quot;);</div><div class="line">// Copy the object to Realm. Any further changes must happen on realmUser</div><div class="line">realm.beginTransaction();</div><div class="line">User realmUser = realm.copyToRealm(user);</div><div class="line">realm.commitTransaction();</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">// Query Realm for all dogs younger than 2 years old</div><div class="line">final RealmResults&lt;Dog&gt; puppies = realm.where(Dog.class).lessThan(&quot;age&quot;, 2).findAll();</div><div class="line">puppies.size(); // =&gt; 0 because no dogs have been added to the Realm yet</div><div class="line">// Persist your data in a transaction</div><div class="line">realm.beginTransaction();</div><div class="line">final Dog managedDog = realm.copyToRealm(dog); // Persist unmanaged objects</div><div class="line">Person person = realm.createObject(Person.class); // Create managed objects directly</div><div class="line">person.getDogs().add(managedDog);</div><div class="line">realm.commitTransaction();</div><div class="line">public void onStop () &#123;</div><div class="line">    if (transaction != null &amp;&amp; !transaction.isCancelled()) &#123;</div><div class="line">        transaction.cancel();</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>​<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div></pre></td><td class="code"><pre><div class="line">// Listeners will be notified when data changes</div><div class="line">puppies.addChangeListener(new RealmChangeListener&lt;RealmResults&lt;Dog&gt;&gt;() &#123;</div><div class="line">    @Override</div><div class="line">    public void onChange(RealmResults&lt;Dog&gt; results) &#123;</div><div class="line">        // Query results are updated in real time</div><div class="line">        puppies.size(); // =&gt; 1</div><div class="line">    &#125;</div><div class="line">&#125;);</div><div class="line"></div><div class="line">// Asynchronously update objects on a background thread</div><div class="line">realm.executeTransactionAsync(new Realm.Transaction() &#123;</div><div class="line">    @Override</div><div class="line">    public void execute(Realm bgRealm) &#123;</div><div class="line">        Dog dog = bgRealm.where(Dog.class).equalTo(&quot;age&quot;, 1).findFirst();</div><div class="line">        dog.setAge(3);</div><div class="line">    &#125;</div><div class="line">&#125;, new Realm.Transaction.OnSuccess() &#123;</div><div class="line">    @Override</div><div class="line">    public void onSuccess() &#123;</div><div class="line">    	// Original queries and Realm objects are automatically updated.</div><div class="line">    	puppies.size(); // =&gt; 0 because there are no more puppies younger than 2 years old</div><div class="line">    	managedDog.getAge();   // =&gt; 3 the dogs age is updated</div><div class="line">    &#125;</div><div class="line">&#125;);</div><div class="line"></div><div class="line">realm.executeTransaction(new Realm.Transaction() &#123;</div><div class="line">    @Override</div><div class="line">    public void execute(Realm realm) &#123;</div><div class="line">        Dog myDog = realm.createObject(Dog.class);</div><div class="line">        myDog.setName(&quot;Fido&quot;);</div><div class="line">        myDog.setAge(1);</div><div class="line">    &#125;</div><div class="line">&#125;);</div><div class="line">​</div></pre></td></tr></table></figure></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">Dog myDog = realm.where(Dog.class).equalTo(&quot;age&quot;, 1).findFirst();</div><div class="line">realm.executeTransaction(new Realm.Transaction() &#123;</div><div class="line">    @Override</div><div class="line">    public void execute(Realm realm) &#123;</div><div class="line">        Dog myPuppy = realm.where(Dog.class).equalTo(&quot;age&quot;, 1).findFirst();</div><div class="line">        myPuppy.setAge(2);</div><div class="line">    &#125;</div><div class="line">&#125;);</div><div class="line">myDog.getAge(); // =&gt; 2</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div></pre></td><td class="code"><pre><div class="line">realm.executeTransactionAsync(new Realm.Transaction() &#123;</div><div class="line">            @Override</div><div class="line">            public void execute(Realm bgRealm) &#123;</div><div class="line">                User user = bgRealm.createObject(User.class);</div><div class="line">                user.setName(&quot;John&quot;);</div><div class="line">                user.setEmail(&quot;john@corporation.com&quot;);</div><div class="line">            &#125;</div><div class="line">        &#125;, new Realm.Transaction.OnSuccess() &#123;</div><div class="line">            @Override</div><div class="line">            public void onSuccess() &#123;</div><div class="line">                // Transaction was a success.</div><div class="line">            &#125;</div><div class="line">        &#125;, new Realm.Transaction.OnError() &#123;</div><div class="line">            @Override</div><div class="line">            public void onError(Throwable error) &#123;</div><div class="line">                // Transaction failed and was automatically canceled.</div><div class="line">            &#125;</div><div class="line">        &#125;);</div><div class="line">public void onStop () &#123;</div><div class="line">    if (transaction != null &amp;&amp; !transaction.isCancelled()) &#123;</div><div class="line">        transaction.cancel();</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div></pre></td><td class="code"><pre><div class="line">// r1 =&gt; [U1,U2]</div><div class="line">RealmResults&lt;Person&gt; r1 = realm.where(Person.class)</div><div class="line">                             .equalTo(&quot;dogs.name&quot;, &quot;Fluffy&quot;)</div><div class="line">                             .equalTo(&quot;dogs.color&quot;, &quot;Brown&quot;)</div><div class="line">                             .findAll();</div><div class="line">// r2 =&gt; [U2]</div><div class="line">RealmResults&lt;Person&gt; r2 = realm.where(Person.class)</div><div class="line">                             .equalTo(&quot;dogs.name&quot;, &quot;Fluffy&quot;)</div><div class="line">                             .findAll()</div><div class="line">                             .where()</div><div class="line">                             .equalTo(&quot;dogs.color&quot;, &quot;Brown&quot;)</div><div class="line">                             .findAll();</div><div class="line">                             .where()</div><div class="line">                             .equalTo(&quot;dogs.color&quot;, &quot;Yellow&quot;)</div><div class="line">                             .findAll();</div><div class="line">// Build the query looking at all users:</div><div class="line">RealmQuery&lt;User&gt; query = realm.where(User.class);</div><div class="line">// Add query conditions:</div><div class="line">query.equalTo(&quot;name&quot;, &quot;John&quot;);</div><div class="line">query.or().equalTo(&quot;name&quot;, &quot;Peter&quot;);</div><div class="line">// Execute the query:</div><div class="line">RealmResults&lt;User&gt; result1 = query.findAll();</div><div class="line">// Or alternatively do the same all at once (the &quot;Fluent interface&quot;):</div><div class="line">RealmResults&lt;User&gt; result2 = realm.where(User.class)</div><div class="line">                                  .equalTo(&quot;name&quot;, &quot;John&quot;)</div><div class="line">                                  .or()</div><div class="line">                                  .equalTo(&quot;name&quot;, &quot;Peter&quot;)</div><div class="line">                                  .findAll();</div><div class="line">RealmResults&lt;User&gt; r = realm.where(User.class)</div><div class="line">                            .greaterThan(&quot;age&quot;, 10)  //implicit AND</div><div class="line">                            .beginGroup()</div><div class="line">                                .equalTo(&quot;name&quot;, &quot;Peter&quot;)</div><div class="line">                                .or()</div><div class="line">                                .contains(&quot;name&quot;, &quot;Jo&quot;)</div><div class="line">                            .endGroup()</div><div class="line">                            .findAll();</div><div class="line">// r1 =&gt; [U1,U2]</div><div class="line">RealmResults&lt;Person&gt; r1 = realm.where(Person.class)</div><div class="line">                             .equalTo(&quot;dogs.name&quot;, &quot;Fluffy&quot;)</div><div class="line">                             .equalTo(&quot;dogs.color&quot;, &quot;Brown&quot;)</div><div class="line">                             .findAll();</div><div class="line">// r2 =&gt; [U2]</div><div class="line">RealmResults&lt;Person&gt; r2 = realm.where(Person.class)</div><div class="line">                             .equalTo(&quot;dogs.name&quot;, &quot;Fluffy&quot;)</div><div class="line">                             .findAll()</div><div class="line">                             .where()</div><div class="line">                             .equalTo(&quot;dogs.color&quot;, &quot;Brown&quot;)</div><div class="line">                             .findAll();</div><div class="line">                             .where()</div><div class="line">                             .equalTo(&quot;dogs.color&quot;, &quot;Yellow&quot;)</div><div class="line">                             .findAll();</div><div class="line">6</div><div class="line">final RealmResults&lt;User&gt; users = getUsers();</div><div class="line">realm.executeTransaction(new Realm.Transaction() &#123;</div><div class="line">    @Override</div><div class="line">    public void execute(Realm realm) &#123;</div><div class="line">        users.deleteFromRealm(0); // Delete and remove object directly</div><div class="line">    &#125;</div><div class="line">&#125;);</div><div class="line">for (User user : users) &#123;</div><div class="line">    showUser(user); // Deleted user will not be shown</div><div class="line">&#125;</div><div class="line">// obtain the results of a query</div><div class="line">final RealmResults&lt;Dog&gt; results = realm.where(Dog.class).findAll();</div><div class="line">// All changes to data must happen in a transaction</div><div class="line">realm.executeTransaction(new Realm.Transaction() &#123;</div><div class="line">    @Override</div><div class="line">    public void execute(Realm realm) &#123;</div><div class="line">        // remove single match</div><div class="line">        results.deleteFirstFromRealm();</div><div class="line">        results.deleteLastFromRealm();</div><div class="line">        // remove a single object</div><div class="line">        Dog dog = results.get(5);</div><div class="line">        dog.deleteFromRealm();</div><div class="line">        // Delete all matches</div><div class="line">        results.deleteAllFromRealm();</div><div class="line">    &#125;</div><div class="line">&#125;);</div><div class="line">As</div><div class="line">7.</div><div class="line">/ Setup Realm in your Application</div><div class="line">public class MyApplication extends Application &#123;</div><div class="line">    @Override</div><div class="line">    public void onCreate() &#123;</div><div class="line">        super.onCreate();</div><div class="line">        RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this).build();</div><div class="line">        Realm.setDefaultConfiguration(realmConfiguration);</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line">// onCreate()/onDestroy() overlap when switching between activities so onCreate()</div><div class="line">// on Activity 2 will be called before onDestroy() on Activity 1.</div><div class="line">public class MyActivity extends Activity &#123;</div><div class="line">    private Realm realm;</div><div class="line">    @Override</div><div class="line">    protected void onCreate(Bundle savedInstanceState) &#123;</div><div class="line">        super.onCreate(savedInstanceState);</div><div class="line">        realm = Realm.getDefaultInstance();</div><div class="line">    &#125;</div><div class="line">    @Override</div><div class="line">    protected void onDestroy() &#123;</div><div class="line">        super.onDestroy();</div><div class="line">        realm.close();</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line">// Use onStart()/onStop() for Fragments as onDestroy() might not be called.</div><div class="line">public class MyFragment extends Fragment &#123;</div><div class="line">    private Realm realm;</div><div class="line">    @Override</div><div class="line">    public void onStart() &#123;</div><div class="line">        super.onStart();</div><div class="line">        realm = Realm.getDefaultInstance();</div><div class="line">    &#125;</div><div class="line">    @Override</div><div class="line">    public void onStop() &#123;</div><div class="line">        super.onStop();</div><div class="line">        realm.close();</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">.</div><div class="line">`</div></pre></td></tr></table></figure>
<p>````</p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持技术分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>Donate</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/uploads/images/wxpay.png" alt="Ernest Chang WeChat Pay"/>
        <p>WeChat Pay</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/uploads/images/alipay.png" alt="Ernest Chang Alipay"/>
        <p>Alipay</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/android/" rel="tag"># android</a>
          
            <a href="/tags/code/" rel="tag"># code</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/04/07/node/angular.js4.0 /" rel="next" title="angular.js4.0学习笔记">
                <i class="fa fa-chevron-left"></i> angular.js4.0学习笔记
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/07/19/git/2016-01-21-git-commit-style/" rel="prev" title="git commit 编写风格模板">
                git commit 编写风格模板 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview sidebar-panel sidebar-panel-active">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/uploads/images/avert.png"
               alt="Ernest Chang" />
          <p class="site-author-name" itemprop="name">Ernest Chang</p>
           
              <p class="site-description motion-element" itemprop="description">All any predator can do is to keep on trying.</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/archives/">
                <span class="site-state-item-count">12</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/categories/index.html">
                <span class="site-state-item-count">8</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">5</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        
          <div class="links-of-blogroll motion-element links-of-blogroll-inline">
            <div class="links-of-blogroll-title">
              <i class="fa  fa-fw fa-globe"></i>
              Links
            </div>
            <ul class="links-of-blogroll-list">
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.jianshu.com/u/9b7137da7356" title="Ernest的简书" target="_blank">Ernest的简书</a>
                </li>
              
            </ul>
          </div>
        

        


      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Ernest Chang</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Pisces
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.2"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.2"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.2"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.2"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.2"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.2"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.2"></script>



  


  




	





  





  






  





  

  

  

  

  

  

</body>
</html>
